package cn.shyouchi.wuguanjia.android.ui.register;

import android.text.InputType;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.model.SmsCodeMod;
import cn.shyouchi.wuguanjia.android.model.User;
import cn.shyouchi.wuguanjia.android.ui.completeinfo.ImproveBasicInfoActivity;

public class RegisterActivity extends BaseActivity implements RegisterContract.View {

    @BindView(R.id.register_telephone_edit_text)
    EditText telephone;
    @BindView(R.id.register_code_edit_text)
    EditText code;
    @BindView(R.id.register_password_edit_text)
    EditText password;
    @BindView(R.id.register_password_confirm_edit_text)
    EditText passwordConfirm;

    @BindView(R.id.security_register_button)
    Button securityRegisterButton;
    @BindView(R.id.property_register_button)
    Button propertyRegisterButton;
    @BindView(R.id.register_fetch_code_button)
    Button fetchCodeButton;
    @BindView(R.id.register_submit_button)
    Button submitButton;

    @BindView(R.id.user_register_tab_bar_container)
    LinearLayout tabBarContainer;

    @BindView(R.id.user_register_read_rule_icon)
    ImageView checkRuleImageView;
    @BindView(R.id.user_register_see_password_icon)
    ImageView seePasswordImageView;
    @BindView(R.id.user_register_see_confirm_password_icon)
    ImageView toggleConfirmPasswordImageView;

    public static final String TAG = "RegisterActivity";
    public static final int SECURITY_REGISTER = 1;
    public static final int PROPERTY_REGISTER = 2;
    private int whoIsRegister;
    private boolean isChecked = false;
    private boolean isShowPassword = false, isShowConfirmPassword = false;
    RegisterPresenter mPresenter;

    SmsCodeMod codeMod;

    @Override
    public int initContentView() {
        return R.layout.activity_register;
    }

    @Override
    public void initInjector() {
        mPresenter = new RegisterPresenter(this);
        mPresenter.attachView(this);
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        whoIsRegister = SECURITY_REGISTER;
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    @OnClick(R.id.register_fetch_code_button)
    void getCode() {
        mPresenter.fetchSmsCode(telephone.getText().toString());
    }


    @OnClick(R.id.register_submit_button)
    void register() {
        mPresenter.register(whoIsRegister,
                telephone.getText().toString(),
                code.getText().toString(),
                password.getText().toString(),
                passwordConfirm.getText().toString(),
                isChecked, codeMod);
    }

    @OnClick(R.id.security_register_button)
    void securityRegisterButtonTapped() {
        tabBarContainer.setBackgroundResource(R.mipmap.bg_user_register_tab_2);
        securityRegisterButton.setTextColor(getResources().getColor(android.R.color.white));
        propertyRegisterButton.setTextColor(getResources().getColor(R.color.mainColor));
        whoIsRegister = SECURITY_REGISTER;
    }

    @OnClick(R.id.user_register_read_rule_icon)
    void readRuleIconTapped() {
        isChecked = !isChecked;
        int resourceId = isChecked ? R.mipmap.ic_check_yes : R.mipmap.ic_remember_password;
        checkRuleImageView.setImageResource(resourceId);
    }

    @OnClick(R.id.property_register_button)
    void propertyRegisterButtonTapped() {
        tabBarContainer.setBackgroundResource(R.mipmap.bg_user_register_tab_1);
        securityRegisterButton.setTextColor(getResources().getColor(R.color.mainColor));
        propertyRegisterButton.setTextColor(getResources().getColor(android.R.color.white));
        whoIsRegister = PROPERTY_REGISTER;
    }

    @OnClick(R.id.user_register_see_password_icon)
    void togglePasswordIconTapped() {
        isShowPassword = !isShowPassword;
        int resourceId = isShowPassword ? R.mipmap.ic_eye_close : R.mipmap.ic_eye_open;
        int inputType = isShowPassword ? InputType.TYPE_TEXT_VARIATION_PASSWORD : InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD;
        seePasswordImageView.setImageResource(resourceId);
        password.setInputType(inputType);
    }

    @OnClick(R.id.user_register_see_confirm_password_icon)
    void toggleConfirmPasswordIconTapeped() {
        isShowConfirmPassword = !isShowConfirmPassword;
        int resourceId = isShowConfirmPassword ? R.mipmap.ic_eye_close : R.mipmap.ic_eye_open;
        int inputType = isShowConfirmPassword ? InputType.TYPE_TEXT_VARIATION_PASSWORD : InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD;
        toggleConfirmPasswordImageView.setImageResource(resourceId);
        passwordConfirm.setInputType(inputType);
    }


    @Override
    public void sendComplete(SmsCodeMod smsCodeMod) {
        codeMod = smsCodeMod;
        showToast("短信发送成功");
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(RegisterActivity.this, msg, Toast.LENGTH_LONG).show();
    }

    @Override
    public void jumpBaseInfo(User user) {
        ImproveBasicInfoActivity.start(mContext, user);
        finish();
    }
}
